package controller;

import entity.Lin;
import org.aspectj.weaver.Lint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.LinService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@Controller
@RequestMapping("/lin")
public class LinCon {
    @Resource(name="linService")
    private LinService linService;

//    查询全部内
    @RequestMapping("/query1")
    @ResponseBody
    public List<Lin> list1(){
        return linService.list1();
    }

//    查询全部外
    @RequestMapping("/query2")
    @ResponseBody
    public List<Lin> list2(){
        return linService.list2();
    }

//    添加
    @RequestMapping("/add")
    @ResponseBody
    public List<Lin> add(Lin l){

        Lin lin = linService.cha(l);

        if(lin!=null){
            lin.setSum(lin.getSum()+l.getSum());
            int p = linService.updnum(lin);
            if(p!=0) {
                return linService.list1();
            }
        }else{
            int i = linService.add(l);
            if(i!=0) {
                return linService.list1();
            }
        }

        return null;
    }

//    修改
    @RequestMapping("/upd")
    @ResponseBody
    public List<Lin> upd(String s,String num){


//        选中的id
        String ss = s.substring(1,s.toCharArray().length-1);
        String [] s2 = ss.split(",");

//        选中内容的编号
        String number = num.substring(1,num.toCharArray().length-1);
        String [] num1 = number.split(",");
        Lin lin = new Lin();
//        循环添加or修改
        for (int i=0;i<s2.length; i++){
//            进行第一遍查询，查看该订单中是否有该商品
            lin.setNumber(num1[i].substring(1,num1[i].toCharArray().length-1));
            lin.setType(2);
            Lin type2 = linService.cha(lin);


//            判断
            if(type2==null){
//                没有，则修改状态
                long p = linService.upd(s2[i]);

            }else{
//                有，修改数量
//                先进行查询
                lin.setType(1);
//                查询到type等于1的商品
                Lin type1 = linService.cha(lin);
//                修改商品的数量
                type1.setSum(type2.getSum()+type1.getSum());
//                执行修改操作
                int pan = linService.updq(type1);
//                删除
                int pp = linService.del(type2.getId());

            }

        }

        return linService.list2();
    }
//    修改数量
    @RequestMapping("/updsum")
    @ResponseBody
    public List<Lin> updsum(Lin l){
        int i = linService.updsum(l);
        return list2();
    }
//    删除
    @RequestMapping("/del")
    @ResponseBody
    public List<Lin> del(int id){
        int del = linService.del(id);
        return linService.list2();
    };


    public int add1(Lin l){


        return 0;
    }


    @RequestMapping("/chanumber")
    @ResponseBody
    public String chanumber(){
        return linService.chanumber();
    }


    @RequestMapping("/add2")
    @ResponseBody
    public List<Lin> add2(Lin l){

        int i = linService.add(l);

        return linService.list2();
    }


    @RequestMapping("/qing")
    @ResponseBody
    public List<Lin> qing(){


        int i = linService.qing();

        return linService.list2();
    }


    @RequestMapping("/datailsadd")
    @ResponseBody
    public List<Lin> add3(String order){

        int i = linService.datailsadd(order);

        if(i>0){
            int j = linService.datailsupd();
            System.out.println(j);
        }


        return linService.list2();
    }


}
